import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 进行普通文件的复制
 * 1.需要知道原路径
 * 2.需要知道目标路径
 */
public class Demo13 {
    public static void main(String[] args) {
        //1.输入两个路径
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要拷贝的原路径：");
        String src = scanner.nextLine();
        File srcFile = new File(src);
        if(srcFile.isDirectory()) {
            System.out.println("输入的路径有误！");
            return;
        }
        System.out.print("请输入目标路径：");
        String dest = scanner.nextLine();
        //不需要检查目标文件是否存在，OutputStream写文件时会自动创建不存在的文件
        //2. 读取源文件，拷贝到目标文件中
        try(InputStream inputStream = new FileInputStream(src)) {
            try(OutputStream outputStream = new FileOutputStream(dest)) {
                //把inputStream中的数据都读出来，写到outputStream中
                byte[] buffer = new byte[1024];
                while(true) {
                    int len = inputStream.read(buffer);
                    if(len == -1) {
                        //拷贝结束
                        System.out.println("拷贝完成！");
                        break;
                    }
                    //需要指定拷贝buffer中的范围
                    outputStream.write(buffer, 0, len);
                }
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
